package cn.hwadee.muying.controller;


import cn.hwadee.muying.service.JobService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import cn.hwadee.muying.commons.base.BaseController;
import cn.hwadee.muying.commons.jqgrid.GridResult;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.ui.Model;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.hwadee.muying.service.WorkerLevelService;
import cn.hwadee.muying.entity.WorkerLevel;
import java.util.HashMap;
import java.util.Map;
import org.apache.shiro.authz.annotation.Logical;

/**
* <p>
* 员工等级 前端控制器
*
* </p>
*
* @author Carlos
* @since 2018-08-07
*/
@Controller
@RequestMapping("/sys/workerLevel")
public class WorkerLevelController extends BaseController {
    @Autowired
    private WorkerLevelService workerLevelService;
    @Autowired
    private JobService jobService;
    @ModelAttribute("workerLevel")
    public WorkerLevel get(@RequestParam(required = false)Long id) throws Exception{
        if(id != null && id > 0){
            return workerLevelService.selectById(id);
        }else{
            return new WorkerLevel();
        }
    }

    @RequiresPermissions("sys:workerLevel:list")
    @RequestMapping(value="/list",produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        return "/biz/workerLevel/list";
    }

    @ResponseBody
    @RequiresPermissions("sys:workerLevel:list")
    @RequestMapping(value="/list.json",produces = "application/json;charset=utf-8")
    public GridResult list(String keyword) throws Exception{
        MyPage<WorkerLevel> page = getPage();
        page = workerLevelService.list(page,getCurrentUser(),keyword);
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    /**
     * 跳转添加/编辑员工等级界面
     * @param workerLevel
     * @param model
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:workerLevel:edit","sys:workerLevel:add"},logical=Logical.OR)
    @RequestMapping(value = {"/edit","/add"}, produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String add(@ModelAttribute WorkerLevel workerLevel,Model model) throws Exception{
        model.addAttribute("workerLevel",workerLevel);
        model.addAttribute("jobs",jobService.selectAll());
        model.addAttribute("levels",workerLevelService.selectAll());
        return "/biz/workerLevel/edit";
    }

    /**
     * 处理更新员工等级的请求
     * @param workerLevel
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:workerLevel:edit","sys:workerLevel:add"},logical=Logical.OR)
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute WorkerLevel workerLevel){
        Map<String,Object> result = new HashMap();
        if(workerLevel.getNextId()!=null){
            workerLevel.setHasNext(true);
        }else{
            workerLevel.setHasNext(false);
        }
        try {
        if(workerLevel.getId() != null && workerLevel.getId() > 0){
            workerLevelService.updateById(workerLevel);
        }else {
            workerLevelService.insert(workerLevel);
        }
        result.put("status",200);
        result.put("msg","员工等级信息保存成功");
        }catch (Exception e){
        e.printStackTrace();
        result.put("status",500);
        result.put("msg","员工等级信息保存失败");
        }
        return result;
    }

    /**
     * 处理删除员工等级的请求
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:workerLevel:delete")
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashMap();
        try {
        if(id != null && id > 0){
            workerLevelService.deleteById(id);
            result.put("status",200);
            result.put("msg","删除员工等级成功");
        }else {
            result.put("status",500);
            result.put("msg","请选择删除的员工等级");
        }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除员工等级失败");
        }
        return result;
    }

}
